
package com.j2eefast.produce.pms.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j2eefast.framework.code.service.SerialService;
import com.j2eefast.common.core.page.Query;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.UserUtils;
import com.j2eefast.produce.pms.entity.PmsChargeStandardListEntity;
import com.j2eefast.produce.pms.entity.PmsProjectEntity;
import com.j2eefast.produce.pms.entity.PmsTestProjectEntity;
import com.j2eefast.produce.pms.mapper.PmsTestProjectMapper;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>检测项目分类列表</p>
 *
 * @author: summer
 * @date: 2021-05-29 16:47
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */
@Service
@Transactional(readOnly = true)
public class PmsTestProjectService extends ServiceImpl<PmsTestProjectMapper, PmsTestProjectEntity> {

	@Resource
	protected PmsTestProjectMapper pmsTestProjectMapper;
	@Resource
	@Lazy
	private SerialService serialService;

	/**
	 * 页面展示查询翻页
	 */
	public PageUtil findPage(Map<String, Object> params) {
		String testCode2=(String)params.get("testCode2");
		String testType2=(String)params.get("testType2");
		String testType=(String)params.get("testType");
		String name=(String)params.get("name");
		String classifyId=(String)params.get("classifyId");
		String name2=(String)params.get("name2");
		String testCode=(String)params.get("testCode");
		Page<PmsTestProjectEntity> page = pmsTestProjectMapper.findPage(
				new Query<PmsProjectEntity>(params).getPage(),
				StrUtil.nullToDefault(testType,""),
				StrUtil.nullToDefault(name,""),
				StrUtil.nullToDefault(classifyId,""),
				StrUtil.nullToDefault(testCode,""),
				StrUtil.nullToDefault(testCode2,""),
				StrUtil.nullToDefault(testType2,""),
				StrUtil.nullToDefault(name2,""),
				(String) params.get(Constant.SQL_FILTER));
		return new PageUtil(page);
	}

	public List<PmsTestProjectEntity> findList(Map<String, Object> params) {
		String testType=(String)params.get("testType");
		String name=(String)params.get("name");
		String classifyId=(String)params.get("classifyId");
		List<PmsTestProjectEntity> page = pmsTestProjectMapper.findList(
				new Query<PmsProjectEntity>(params).getPage(),
				StrUtil.nullToDefault(testType,""),
				StrUtil.nullToDefault(name,""),
				StrUtil.nullToDefault(classifyId,""),
				(String) params.get(Constant.SQL_FILTER));
		return page;
	}


	@Transactional(rollbackFor = Exception.class)
	public boolean deleteByIds(Long[] ids) {

		return pmsTestProjectMapper.deleteByIds(ids);

	}
	@Transactional(rollbackFor = Exception.class)
	public boolean updateCheck(Long id) {
		Long userId= UserUtils.getUserInfo().getId();

		return pmsTestProjectMapper.updateCheck(id ,userId);

	}
	/**
	 * 添加
	 *
	 */
	@Transactional(rollbackFor = Exception.class)
	public boolean saves(PmsTestProjectEntity taskFrom){
//		Long userId= UserUtils.getUserInfo().getId();
		Date date = new Date();//创建一个时间对象，获取到当前的时间
		String userName=UserUtils.getUserInfo().getName();
		taskFrom.setUpdateTime(date);
		taskFrom.setCreateTime(date);
		taskFrom.setCreateBy(userName);
		taskFrom.setUpdateBy(userName);
		PmsTestProjectEntity pmsTestProjectEntity = pmsTestProjectMapper.selectTestProject();
		Long total=0L;
		if(pmsTestProjectEntity!=null && pmsTestProjectEntity.getTotal()!=null){
			total=pmsTestProjectEntity.getTotal();
		}
		Map<String ,String> participant=serialService.formatTitle(total,"检测项目编号序列");//编号
		taskFrom.setTestCode(participant.get("title"));
		taskFrom.setTotal(Long.parseLong(participant.get("total")));
		return this.save(taskFrom);

	}

	public List<PmsTestProjectEntity> getsProjectList(Map<String, Object> params){
		String productName=(String)params.get("productName");
		return pmsTestProjectMapper.getsProjectList(productName);
	}



}
